﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Blizzard.Contracts;
using System.Reflection;
using System.Collections;
using Blizzard.Container;

namespace Blizzard
{
    internal class Helper
    {

   
 
    
        internal static bool IsAList(Type type)
        {
            bool IsList = false;

            Type[] interfaces = type.GetInterfaces();

            foreach (Type t in interfaces)
            {
                if (t == typeof(IList))
                    IsList = true;
            }

            return IsList;
        }

        /// <summary>
        /// Returns The Generic type argument of a genericType
        /// (if generic type has more than one generic argument it returns only the first one)
        /// </summary>
        /// <param name="genericType"></param>
        /// <returns></returns>
        internal static Type GetTypeOfGenericArgument(Type genericType)
        {
            Type genericArgument = null;

            Type[] generics = genericType.GetGenericArguments();
            
            if (generics.Length != 0)
            {
                genericArgument = generics[0];
            }


            return genericArgument;
        }
     
        internal static bool ImplementsIDataObjects(Type type)
        {
            bool ret = false;

            Type[] interfaces = type.GetInterfaces();

            foreach (Type interf in interfaces)
            {
                if (interf == typeof(IDataObject))
                {
                    ret = true;
                }
            }

            return ret;
        }


        internal static string GetPropertyName(IDataObject mainObject, Type AssociateIDataObjectType)
        {
            string retName = "";
            foreach (PropertyInfo pi in mainObject.GetType().GetProperties())
            {
                if (pi.PropertyType == AssociateIDataObjectType)
                    retName = pi.Name;
            }

            return retName;
        }

        internal static Role GetMyRoleInRelationShip(Type MyType, RelationDefinition rd)
        {
            
            if (rd.TypeRole1.MyType == MyType)
            {
               return rd.TypeRole1.MyRole;
            }
            else if (rd.TypeRole2.MyType == MyType)
            {
               return rd.TypeRole2.MyRole;
            }
            else
            {
                throw new Exception("No Role has been found (There is no type match)");
            }
        }

        internal static Type GetAssociatedTypeInRelationShip(Type MyType, RelationDefinition rd)
        {

            
            if (rd.TypeRole1.MyType == MyType)
            {
               return rd.TypeRole2.MyType;
            }
            else if (rd.TypeRole2.MyType == MyType)
            {
                return rd.TypeRole1.MyType;
            }
            else
            {
                throw new Exception("No type has been found (There is no type match)");
            }
        }
    
    }
}
